﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace Ession
{
    public class MailSender
    {
        public MailSender()
        {

        }

          public MailSender(string SenderMail, string SenderName,
            string ReceiveUserName, string ReceiveEmail, string Subject,
            string body, string smtpClient, string senderMailName, string senderPwd)
        {
            this.SenderMail = SenderMail;
            this.SenderName = SenderName;
            this.senderMailName = senderMailName;
            this.ReceiveUserName = ReceiveUserName;
            this.ReceiveEmail = ReceiveEmail;
            this.senderPwd = senderPwd;
            this.body = body;
            this.smtpClient = smtpClient;
            this.Subject = Subject;


        }
          public string SenderMail, SenderName, ReceiveUserName, ReceiveEmail,
         Subject, body, smtpClient, senderMailName, senderPwd, IP;
        public bool SendEmail(string To_mail, string mail_title, string body, string smtp, string fromMainAddress, string userName, string pwd)  //发送邮件接口
        {

            MailMessage mymail = new MailMessage();
            mymail.From = new System.Net.Mail.MailAddress(fromMainAddress);
            mymail.To.Add(To_mail);
            mymail.Subject = mail_title;
            mymail.Body = body;
            mymail.IsBodyHtml = true;

            SmtpClient smtpclient = new SmtpClient();
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpclient.Host = smtp;
            smtpclient.Credentials = new System.Net.NetworkCredential(userName, pwd);
            try
            {
                smtpclient.Send(mymail);
                return true;
            }
            catch (Exception ex)
            {
                
                Console.WriteLine(ex.ToString());
                return false;
            }
        }
        public bool SendEmailForGetpass()
        {


            MailAddress from = new MailAddress(this.SenderMail, this.SenderName);
            MailAddress to = new MailAddress(this.ReceiveEmail, this.ReceiveUserName);
            MailMessage message = new MailMessage(this.SenderMail, this.ReceiveEmail);
            message.Subject = this.Subject;
            message.Body = this.body;
            message.Priority = MailPriority.High;
            message.IsBodyHtml = true;

            SmtpClient client = new SmtpClient(this.smtpClient);
            client.Credentials = new System.Net.NetworkCredential(this.senderMailName, this.senderPwd);
            try
            {
                client.Send(message);

                return true;
            }
            catch (Exception e)
            {

            }
            return false;
        }
        public bool SendEmailForGetpass(string SenderMail, string SenderName, string ReceiveUserName, string ReceiveEmail, string Subject, string body, string smtpClient, string senderMailName, string senderPwd)
        {
            //   TheMail mail = new TheMail(SenderMail, SenderName, ReceiveUserName, ReceiveEmail,
            //Subject, body, smtpClient, senderMailName, senderPwd);

            //   Thread th = new Thread(new ParameterizedThreadStart(sendMail));
            //   th.Start(mail);



            MailAddress from = new MailAddress(SenderMail, SenderName);
            MailAddress to = new MailAddress(ReceiveEmail, ReceiveUserName);
            MailMessage message = new MailMessage(from, to);
            message.Subject = Subject;
            message.Body = body;
            message.Priority = MailPriority.High;
            message.IsBodyHtml = true;

            SmtpClient client = new SmtpClient(smtpClient);
            client.Credentials = new System.Net.NetworkCredential(senderMailName, senderPwd);
            try
            {
                client.Send(message);

                return true;
            }
            catch (Exception e)
            {

            }
            return false;
        }
       
    }
}
